package com.company.zoj;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by v on 17-6-7.
 */
public class ZojTest1221 {
    private static int[][] distance=new int[20][20];
    private static List<List<Integer>> lists;

    public static void init(){
        for(int i=0;i<distance.length;i++){
            for(int j=0;j<distance.length;j++){
                distance[i][j]=Integer.MAX_VALUE/2-2;
            }
        }

        for(int i=0;i<distance.length;i++){
            distance[i][i]=0;
        }

        for(int i=0;i<lists.size();i++){
            for(int j=0;j<lists.get(i).size();j++){
                distance[i][lists.get(i).get(j)]=1;
            }
        }
    }

    public static void floyd(){
        for(int k=0;k<20;k++){
            for(int i=0;i<20;i++){
                for(int j=0;j<20;j++){
                    distance[i][j]=Math.min(distance[i][j],
                            distance[i][k]+distance[k][j]);
                }
            }
        }
    }

    public static void main(String[] args) {
        int test=0;
        Scanner input=new Scanner(System.in);
        try {
            while (true){
                test++;
                lists=new ArrayList<List<Integer>>();
                for(int i=0;i<20;i++){
                    lists.add(new LinkedList<Integer>());
                }
                for(int i=0;i<19;i++){
                    int in=input.nextInt();
                    for(int j=0;j<in;j++){
                        int current=input.nextInt()-1;
                        lists.get(i).add(current);
                        lists.get(current).add(i);
                    }
                }

                init();
                floyd();

                int n=input.nextInt();
                System.out.println("Test Set #"+test);
                for(int i=0;i<n;i++){
                    int begin=input.nextInt()-1;
                    int end=input.nextInt()-1;

                    System.out.println((begin+1)+" to "+(end+1)+": "+distance[begin][end]);
                }
                System.out.println();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
